﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace LenderClearingHouse
{
  public partial class DocumentFolderList : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void grid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
      GridEditableItem editedItem = e.Item as GridEditableItem;
      DocumentFolderEditControl userControl = (DocumentFolderEditControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

      string folderName = (userControl.FindControl("tbFolderName") as TextBox).Text;
      string prefix = (userControl.FindControl("tbPrefix") as TextBox).Text;
      
      string investorAdminRole = "NA";
        if ((userControl.FindControl("chkInvestorAdmin") as CheckBox).Checked)
          investorAdminRole = (userControl.FindControl("ddlInvestorAdmin") as DropDownList).SelectedValue;

      string investorRole = "NA";
        if ((userControl.FindControl("chkInvestor") as CheckBox).Checked)
          investorRole = (userControl.FindControl("ddlInvestor") as DropDownList).SelectedValue;

      string userRole = "NA";
        if ((userControl.FindControl("chkUser") as CheckBox).Checked)
          userRole = (userControl.FindControl("ddlUser") as DropDownList).SelectedValue;

      string escrowUserRole = "NA";
        if ((userControl.FindControl("chkEscrowUser") as CheckBox).Checked)
          escrowUserRole = (userControl.FindControl("ddlEscrowUser") as DropDownList).SelectedValue;
      
      DataAccess da = new DataAccess();
      da.ExecuteProcedure("insertDocumentFolder",
        new string[] { "@FolderName", "@Prefix", "@InvestorAdminRole", "@InvestorRole", "@UserRole", "@EscrowUserRole"},
        new object[] { folderName, prefix, investorAdminRole, investorRole, userRole, escrowUserRole});
    }

    protected void grid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
      GridEditableItem editedItem = e.Item as GridEditableItem;
      DocumentFolderEditControl userControl = (DocumentFolderEditControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

      int folderId = int.Parse((userControl.FindControl("hfFolderId") as HiddenField).Value);
      string folderName = (userControl.FindControl("tbFolderName") as TextBox).Text;
      string prefix = (userControl.FindControl("tbPrefix") as TextBox).Text;

      string investorAdminRole = "NA";
      if ((userControl.FindControl("chkInvestorAdmin") as CheckBox).Checked)
        investorAdminRole = (userControl.FindControl("ddlInvestorAdmin") as DropDownList).SelectedValue;

      string investorRole = "NA";
      if ((userControl.FindControl("chkInvestor") as CheckBox).Checked)
        investorRole = (userControl.FindControl("ddlInvestor") as DropDownList).SelectedValue;

      string userRole = "NA";
      if ((userControl.FindControl("chkUser") as CheckBox).Checked)
        userRole = (userControl.FindControl("ddlUser") as DropDownList).SelectedValue;

      string escrowUserRole = "NA";
      if ((userControl.FindControl("chkEscrowUser") as CheckBox).Checked)
        escrowUserRole = (userControl.FindControl("ddlEscrowUser") as DropDownList).SelectedValue;

      DataAccess da = new DataAccess();
      da.ExecuteProcedure("updateDocumentFolder",
        new string[] { "@FolderId",  "@FolderName", "@Prefix", "@InvestorAdminRole", "@InvestorRole", "@UserRole", "@EscrowUserRole" },
        new object[] { folderId, folderName, prefix, investorAdminRole, investorRole, userRole, escrowUserRole });
    }

    protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
    {
      if (e.Item is GridPagerItem)
      {
        GridPagerItem item = e.Item as GridPagerItem;
        RadComboBox combo = (RadComboBox)item.FindControl("PageSizeComboBox");

        RadComboBoxItem newItem = combo.Items.FindItemByValue("100");
        if (newItem == null)
        {
          RadComboBoxItem citem = new RadComboBoxItem("100", "100");
          citem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
          combo.Items.Add(citem);
        }
        else
        {
          newItem.Text = "100";
        }

        newItem = combo.Items.FindItemByValue("1000000");
        if (newItem == null)
        {
          RadComboBoxItem citem = new RadComboBoxItem("ALL", "1000000");
          citem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
          combo.Items.Add(citem);
        }
        else
        {
          newItem.Text = "ALL";
        }
      }
      
    }

    protected void grid_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.EditCommandName || e.CommandName == RadGrid.DeleteCommandName)
        {
            string folder = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["FolderName"].ToString();
            e.Canceled = folder.ToUpper() == "gallery".ToUpper();

            if (e.Canceled)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(),
                    "key", string.Format("alert('This is read-only folder');"), true);
            }
        }
    }
  }
}